package net.fdukedom.epicurus.presentation.actions.pub;

import net.fdukedom.epicurus.presentation.actions.AbstractForm;
import net.fdukedom.epicurus.domain.util.UserHelper;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import javax.servlet.http.HttpServletRequest;

/**
 * Bean for register form.
 *
 * @author Dmitry Y. Kochelaev
 */
public class RegisterForm extends AbstractForm {

    /**
	 * User name.
	 */
	private String userName;

    /**
	 * User email.
	 */
	private String email;

    /**
	 * User password.
	 */
	private String password;

    /**
	 * User password repeat.
	 */
	private String passwordRepeat;

    /**
     * User sex.
     */
    private Short sex;

    public ActionErrors doValidate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        boolean exists = UserHelper.getInstance().findByEmailOrName(email, userName);
        if (exists) {
            errors.add("error.register.general", new ActionMessage("register.errors.alreadyExists"));
        }
        if (userName.length() == 0) {
            errors.add("error.register.login", new ActionMessage("register.errors.emptyLogin"));
        }
        if (password.length() == 0) {
            errors.add("error.register.password", new ActionMessage("register.errors.shortPassword"));
        }
        if (!password.equals(passwordRepeat)) {
            errors.add("error.register.passwordRepeat", new ActionMessage("register.errors.passwordsDoNotMatch"));
        }
        return errors;
    }

    public void doReset(ActionMapping mapping, HttpServletRequest request) {
        userName = "";
        email = "";
        password = "";
        passwordRepeat = "";
        sex = 0;
    }

    /**
	 * Returns user name.
	 *
	 * @return root login
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * Sets user name.
	 *
	 * @param userName user name
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

    /**
     * Returns user email.
     *
     * @return user email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Sets user email.
     *
     * @param email user email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Returns user password.
     *
     * @return user password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Sets user password.
     *
     * @param password user password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Returns user password repeat.
     *
     * @return user password repeat
     */
    public String getPasswordRepeat() {
        return passwordRepeat;
    }

    /**
     * Sets user password repeat.
     *
     * @param passwordRepeat user password repeat
     */
    public void setPasswordRepeat(String passwordRepeat) {
        this.passwordRepeat = passwordRepeat;
    }

    /**
     * Returns user sex.
     *
     * @return user sex
     */
    public Short getSex() {
        return sex;
    }

    /**
     * Sets user sex.
     *
     * @param sex user sex
     */
    public void setSex(Short sex) {
        this.sex = sex;
    }
}
